package com.kingdomi.emquantstarter.service;

import cn.hutool.json.JSONUtil;
import com.eastmoney.api.UserAPI;
import com.eastmoney.callback.user.UserCallBack;
import com.eastmoney.callback.user.UserDataCallBack;
import com.eastmoney.callback.user.UserLogCallBack;
import com.eastmoney.constant.ErrorType;
import com.eastmoney.entity.EQData;
import com.eastmoney.entity.EQMSG;
import com.eastmoney.thread.ExplainClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Author: Jiangxx
 * @Date: 2022/05/17
 * @Description:
 */
@Service
public class EmQuantCase {

    private static final Logger log = LoggerFactory.getLogger(EmQuantCase.class);

    private static UserAPI userAPI;

    @Value("${emquantapi.libpath.serverlistdir}")
    private String serverListdir;

    @Value("${emquantapi.libpath.mode}")
    private String mode;

    @Value("${emquantapi.libpath.phoneNumber}")
    private String phoneNumber;

    @Value("${emquantapi.libpath.wraper}")
    private String wraper;

    @Value("${emquantapi.libpath.dll}")
    private String dll;

    @Value("${emquantapi.manualactivate.run}")
    private boolean manualactivateRun;

    @Value("${emquantapi.manualactivate.userName}")
    private String manualactivateUserName;

    @Value("${emquantapi.manualactivate.password}")
    private String manualactivatePassword;

    @Value("${emquantapi.manualactivate.options}")
    private String manualactivateOptions;

    /**
     * jvm缓存
     */
    private static HashMap<Integer,String> cache;

    static{
        if(cache == null){
            cache = new HashMap<>();
        }
    }

    public void init() {
        HashMap<String, UserCallBack> userCallBackHashMap = new HashMap<String, UserCallBack>();
        userCallBackHashMap.put("msg", new UserDataCallBack() {
            @Override
            public void callback(EQMSG eqmsg) {
                if (eqmsg.getRequestID() == 10000) {
                } else if (eqmsg.getRequestID() == 10001) {
                    log.info("cst callback:" + eqmsg.toString());
                } else if (eqmsg.getRequestID() == 10002) {
                    log.info("cnq callback:" + eqmsg.toString());
                } else if (eqmsg.getRequestID() == 10003) {
                    log.info("chq callback:" + eqmsg.toString());
                }
            }
        });
        userCallBackHashMap.put("log", new UserLogCallBack() {
            @Override
            public void callback(String o) {
                log.info(o);
            }
        });
        ExplainClient.start(userCallBackHashMap);
        //wraperDir = "D:/kd-project/EmQuantAPITest/EmQuantAPITest/Wraper/";
        userAPI = new UserAPI(wraper, ExplainClient.getSystemTaskQueue());
        try {
            //dllDir = "D:/kd-project/EmQuantAPITest/EmQuantAPITest/dll/";
            userAPI.setJniEnv(dll);
        } catch (Exception e) {
            log.error("QuantCase类设置环境变量异常！", e);
        }
    }

    /**
     * 登录接口
     */
    public void start() {
        if (manualactivateRun) {
            log.info("Manualactivate...");
            this.testManualactivate(manualactivateUserName, manualactivatePassword, manualactivateOptions);
            log.info("Manualactivate end!");
            return;
        }
        String options = "LoginMode=" + mode + ",PhoneNumber=" + phoneNumber;
        userAPI.setserverlistdir(serverListdir);
        int result2 = userAPI.start(options);
        if (result2 != 0) {
            log.info("东财系统登录失败，错误码==" + ErrorType.getErrorString(result2));
        } else {
            log.info("东财系统登录成功！");
        }
    }

    /**
     * 人工激活
     * @param userName
     * @param password
     * @param options
     */
    public void testManualactivate(String userName,String password,String options) {
        int result = userAPI.manualactivate(userName, password, options);
        log.info(ErrorType.getErrorString(result));
    }

    public EQData sector(String pukeycode, String endDate, String options) {
        EQData sector = userAPI.sector(pukeycode, endDate, options);
        log.info("东财系统sector()接口返回内容=={}", JSONUtil.toJsonStr(sector));
        if (sector.getErrCode() != 0) {
            log.info("东财系统sector()接口返回Error: " + sector.getErrString());
            return null;
        } else {
            return sector;
        }
    }
}
